//
//  WBVisitorView.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/5/25.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit


class WBVisitorView: UIView {
    
    //TODO: - 访客信息 使用字典设置 (字典可能为空，设置为可选)
    var visitorInfo : [String : String]?{
        didSet{
            /// - guard let 用来判断为空，直接返回,直接走下面就行
            guard let imageName = visitorInfo?["imageName"],
            let message = visitorInfo?["message"] else {
                return
            }
           
            tipLabel.text = message
            /// - 首页图片不需要再传，已经设置好，直接为空
            if imageName == "" {
                startAnimation()
                return
                
            }else{
                
            iconImage.image = UIImage(named:imageName)
            /// - 不是首页都不显示
            maskIconImage.isHidden = true
            houseImage.isHidden = true
           }
        }
    }
    
    
    
    
/// - 初始化 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnimation()  {
        let basicAnima = CABasicAnimation(keyPath: "transform.rotation")
        /// - 从哪里到哪里
        basicAnima.toValue = 2 * M_PI
        /// - 选择次数
        basicAnima.repeatCount = MAXFLOAT
        /// - 一圈时间
        basicAnima.duration = 15
        /// - 完成之后不删除 
        basicAnima.isRemovedOnCompletion = false
        /// - 将动画添加到图层
        iconImage.layer.add(basicAnima, forKey: nil)
    }
    
    
    /// - 懒加载
    /// - 图像
    lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var maskIconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// - 小房子
    lazy var houseImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// - 提示标签
    lazy var tipLabel:UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
 
    
    /// - 注册
    lazy var registerBtn:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    /// - 登录
    lazy var loginBtn:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")

}

//MARK: -  设置页面
extension WBVisitorView{

    func setUpUI()  {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        /// - 1. 添加控件
        addSubview(iconImage)
        addSubview(maskIconImage)
        addSubview(houseImage)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        tipLabel.textAlignment = .center
        /// - 2. 取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        /**
         item: 本控件,
         attribute: .centerX,
         relatedBy: .equal,
         /**
         lessThanOrEqual 小于或等于
         equal 等于
         greaterThanOrEqual 大于或等于

         */
         toItem: 父控件,
         attribute: .centerX,
         multiplier: 1.0 倍率,
         constant: 恒值（X： +右平移 - 左平移 Y: + 下平移 - 上平移）
         */
        
        /// - 3. 自动布局
         /// - 图像
        addConstraint(NSLayoutConstraint(item: iconImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))

        addConstraint(NSLayoutConstraint(item: iconImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        /// - 小房子
        addConstraint(NSLayoutConstraint(item: houseImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        /// - 提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))

        addConstraint(NSLayoutConstraint(item: tipLabel,
                                        attribute: .top,
                                        relatedBy: .equal,
                                        toItem: iconImage,
                                        attribute: .bottom,
                                        multiplier: 1.0,
                                        constant: 20.0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        /// - 注册
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        /// - 登录
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: -10))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        /// - 遮盖
        /// - views: 定义 VFL 中控件名称和实际名称影射关系
        /// - metrics: 定义 VFL中 () 指定的常数影射关系
        let viewDict = ["maskIconImage":maskIconImage
            ,"registerBtn":registerBtn] as [String : Any]
        let metrics = ["spacing":-35]
        
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconImage]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconImage]-(spacing)-[registerBtn]",
            options: [],
            metrics: metrics,
            views: viewDict))
        
    }
    
}
